
from sqlalchemy.orm import DeclarativeBase, relationship
from sqlalchemy import Column, Integer, String, Float, DOUBLE, ForeignKey, Boolean, DateTime, DECIMAL, Enum
class Base(DeclarativeBase):
    pass

class User(Base):
    __tablename__ = 'users'
    u_id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(20), nullable=False)
    password = Column(String(20), nullable=False)
    phone = Column(String(20), nullable=False)
    is_admin = Column(Boolean, default=False)
    register_date = Column(DateTime, default=None)
    card_number = Column(String(20), default=None)
    points = Column(Integer, default=0)
    last_login_date = Column(DateTime, default=None)
    is_deleted = Column(Boolean, default=False)


class Device(Base):
    __tablename__ = 'device'
    d_id = Column(Integer, primary_key=True, autoincrement=True)
    category_name = Column(String(20), nullable=False)
    install_location = Column(String(100), nullable=False)
    qr_code = Column(String(255), nullable=False)
    is_functional = Column(Boolean, default=True)

class Order(Base):
    __tablename__ = 'orders'
    order_id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, ForeignKey('users.u_id'), nullable=False)
    device_id = Column(Integer, ForeignKey('device.d_id'), nullable=False)
    consume_time = Column(DateTime, nullable=False)
    consume_amount = Column(DECIMAL(10, 2), nullable=False)
    payment_method = Column(Enum('card', 'alipay'), nullable=False)
    # user = relationship("User", back_populates="orders")
    # device = relationship("Device", back_populates="orders"

